import tensorflow as tf
from sklearn import datasets
import numpy as np
from tensorflow import keras

x_train = datasets.load_iris().data
y_train = datasets.load_iris().target

np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)

model_path = "iris.h5"

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])

model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

history = model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20,
                    callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='min'),
                               keras.callbacks.ModelCheckpoint(model_path, monitor='val_loss', save_best_only=True, mode='min', verbose=0)])

model.summary()

'''
model_path = "models/" + source + "-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"
#model_path = "models/tongji-1.0-1-5.h5"

model = rfg.create_model_2(dynamic.shape[1:], ob_win)

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', pm.AUC])

dynamic_dim = dynamic.reshape(dynamic.shape[0], dynamic.shape[1], dynamic.shape[2], 1)

history = model.fit([dynamic_dim, dynamic], label, epochs=200, batch_size=1024,class_weight=cw,
                    validation_split=0.3, verbose=2,
                    callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='min'),
                               keras.callbacks.ModelCheckpoint(model_path, monitor='val_loss', save_best_only=True, mode='min', verbose=0)])

if(str(flag)=="BT"):
                        model_path = "/home/mount/chy/models_all/" + source + "+BT-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"
                        f = "test_result+BT.txt"#文件名
                        whether = "True"
                    if not os.path.exists(model_path):
                        print("model is not exist...")
                        os._exit(0)
                    model = keras.models.load_model(model_path, custom_objects={"AUC": pm.AUC})

'''
